package com.tarim.streetdance.service.impl;

import com.tarim.streetdance.mapper.VideoMapper;
import com.tarim.streetdance.model.*;
import com.tarim.streetdance.service.*;
import com.tarim.streetdance.vo.Criteria;
import com.tarim.streetdance.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private TypeService typeService;
    @Autowired
    private VideoTypeService videoTypeService;
    @Autowired
    private UserService userService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertSelective(VideoWithBLOBs videoWithBLOBs) {
        videoWithBLOBs.setCollectnum(0L);
        videoWithBLOBs.setLikenum(0L);
        videoWithBLOBs.setViewsnum(0L);
        videoWithBLOBs.setStatus(0);
        videoWithBLOBs.setDr(0);
        videoWithBLOBs.setCreateTime(new Date());
        int i = videoMapper.insertSelective(videoWithBLOBs);
        List<Type> typeList = videoWithBLOBs.getTypeList();
        List<VideoType> videoTypeList = new ArrayList<>();
        for (Type type:typeList){
            VideoType videoType = new VideoType();
            videoTypeList.add(videoType);
            videoType.setTypeId(type.getId());
            videoType.setVideoId(videoWithBLOBs.getId());
        }
        videoTypeService.insertList(videoTypeList);
        return i;
    }

    @Override
    public PageBean<VideoWithBLOBs> selectVideoByType(PageBean<VideoWithBLOBs> pageBean, VideoWithBLOBs record) {
        Integer pageIndex = pageBean.getPageIndex();
        Integer pageSize = pageBean.getPageSize();
        record.addExtField("pageIndex", pageIndex);
        record.addExtField("pageSize", pageSize);
        List<VideoWithBLOBs> list = videoMapper.selectVideoByType(record);
        Long count = videoMapper.selectVideoByTypeCount(record);
        pageBean.setTotalCount(count);
        pageBean.setData(list.toArray());
        return pageBean;
    }

    @Override
    public VideoWithBLOBs selectByPrimaryKey(Long id) {
        VideoWithBLOBs videoWithBLOBs = videoMapper.selectByPrimaryKey(id);
        for (CommentStair commentStair : videoWithBLOBs.getCommentStairList()) {
            User user1 = userService.selectByPrimaryKey(commentStair.getUserId());
            commentStair.setUser(user1);
            commentStair.setInputShow(false);
            if (commentStair.getCommentSecondList() != null){
                for (CommentSecond commentSecond : commentStair.getCommentSecondList()) {
                    User puser = userService.selectByPrimaryKey(commentSecond.getPuserId());
                    User user = userService.selectByPrimaryKey(commentSecond.getUserId());
                    commentSecond.setPuser(puser);
                    commentSecond.setUser(user);
                    commentSecond.setInputShow(false);
                }
            }
        }
        videoWithBLOBs.setUser(userService.selectByPrimaryKey(videoWithBLOBs.getUserId()));
        return videoWithBLOBs;
    }

    @Override
    public int updateForDelete(Video record) {
        return videoMapper.updateForDelete(record);
    }

    @Override
    public int updateForAddLikenum(Video record) {
        return videoMapper.updateForAddLikenum(record);
    }

    @Override
    public int updateForAddViewsnum(Video record) {
        return videoMapper.updateForAddViewsnum(record);
    }

    @Override
    public int updateForAddCollectnum(Video record) {
        return videoMapper.updateForAddCollectnum(record);
    }

    @Override
    public List<Map> selectStateNum(){
        return videoMapper.selectStateNum();
    }

    @Override
    public PageBean<VideoWithBLOBs> selectVideoByUserId(PageBean<VideoWithBLOBs> pageBean, VideoWithBLOBs record) {
        Integer pageIndex = pageBean.getPageIndex();
        Integer pageSize = pageBean.getPageSize();
        record.addExtField("pageIndex", pageIndex * pageSize);
        record.addExtField("pageSize", pageSize);
        List<VideoWithBLOBs> list = videoMapper.selectVideoByUserId(record);
        Long count = videoMapper.selectVideoByUserIdCount(record);
        pageBean.setTotalCount(count);
        pageBean.setData(list.toArray());
        return pageBean;
    }

    @Override
    public PageBean<VideoWithBLOBs> selectVideoByCondition(PageBean<VideoWithBLOBs> pageBean, Criteria criteria) {
        Integer pageIndex = pageBean.getPageIndex();
        Integer pageSize = pageBean.getPageSize();
        criteria.addExtField("pageIndex", pageIndex * pageSize);
        criteria.addExtField("pageSize", pageSize);
        List<VideoWithBLOBs> list = videoMapper.selectVideoByCondition(criteria);
        Long count = videoMapper.selectVideoByConditionCount(criteria);
        pageBean.setTotalCount(count);
        pageBean.setData(list.toArray());
        return pageBean;
    }

    @Override
    public int updateByPrimaryKeySelective(VideoWithBLOBs video) {
        video.setModifyTime(new Date());
        return videoMapper.updateByPrimaryKeySelective(video);
    }
}